package com.taiyi.renaissancemall.ui.mine.withdraw.bindaccount;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.RegexUtil;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.ui.mine.withdraw.WithdrawSettingActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class BindAccountActivity extends BaseActivity<BindAccountView, BindAccountPresent> implements BindAccountView {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.aliPayAccount)
    TextView aliPayAccount;
    @BindView(R.id.aliPayAccountEt)
    EditText aliPayAccountEt;
    @BindView(R.id.phoneNumber)
    TextView phoneNumber;
    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.verificationCode)
    TextView verificationCode;
    @BindView(R.id.getVerificationCodeTv)
    TextView getVerificationCodeTv;
    @BindView(R.id.verificationCodeEt)
    EditText verificationCodeEt;
    @BindView(R.id.commitTv)
    TextView commitTv;
    @BindView(R.id.top)
    ConstraintLayout top;
    private String bType;
    private TimeCount timeCount;
    private String getOpen_id;
    private String phone;
    private String getNick_name;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_account;
    }

    @Override
    public BindAccountPresent initPresenter() {
        return new BindAccountPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        bType = getIntent().getStringExtra("txtype");
        getOpen_id = getIntent().getStringExtra("getOpen_id");
        phone = getIntent().getStringExtra("phone");
        getNick_name = getIntent().getStringExtra("getNick_name");
        if (phone != null && !TextUtils.isEmpty(phone)) {
            phoneNumberEt.setText(RegexUtil.phoneNoHide(phone));
            phoneNumberEt.setEnabled(false);
        }
        if ("1".equals(bType)) {
            titleTv.setText("绑定微信账户");
            aliPayAccount.setText("微信账户");
            aliPayAccountEt.setText(getNick_name);
            aliPayAccountEt.setEnabled(false);
        } else if ("2".equals(bType)) {
            titleTv.setText("绑定支付宝账户");
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        timeCount = new TimeCount(60000, 1000);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void bindAccountSuccess(String msg_cn) {
        ToastUtils.showShortToast(msg_cn);
        hideProgress();
        Intent intent = new Intent(BindAccountActivity.this, WithdrawSettingActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void bindAccountFailure(String msg) {
        hideProgress();
        ToastUtils.showShortToast(msg);
    }

    @Override
    public void getVerificationSuccess() {
        hideProgress();
        timeCount.start();
    }

    @Override
    public void getVerificationFailure() {
        hideProgress();
    }

    @OnClick({R.id.backIv, R.id.getVerificationCodeTv, R.id.commitTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.getVerificationCodeTv:
                showProgress();
                if (!TextUtils.isEmpty(phoneNumberEt.getText().toString().trim())
                        && RegexUtil.isMobileNO(phoneNumberEt.getText().toString().trim())) {
                    presenter.getVerificationCode(phoneNumberEt.getText().toString().trim());
                } else {
                    if (phone != null && !TextUtils.isEmpty(phone)) {
                        presenter.getVerificationCode(phone);
                    } else {
                        hideProgress();
                        ToastUtils.showLongToast("请输入正确的手机号");
                    }
                }
                break;
            case R.id.commitTv:
                String tel = "";
                if (phone != null && !TextUtils.isEmpty(phone)) {
                    tel = phone;
                } else {
                    tel = phoneNumberEt.getText().toString().trim();
                }
                if (TextUtils.isEmpty(aliPayAccountEt.getText().toString().trim())) {
                    ToastUtils.showLongToast("请输入账号");
                    return;
                }
                if (TextUtils.isEmpty(tel)) {
                    ToastUtils.showLongToast("请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(verificationCodeEt.getText().toString().trim())) {
                    ToastUtils.showLongToast("请输入验证码");
                    return;
                }
                showProgress();
                if ("1".equals(bType)) {
                    presenter.toBindAccount(getOpen_id, getNick_name, bType, MyApp.getDeviceUUid(), verificationCodeEt.getText().toString().trim());
                } else if ("2".equals(bType)) {
                    presenter.toBindAccount(aliPayAccountEt.getText().toString().trim(), "", bType, MyApp.getDeviceUUid(), verificationCodeEt.getText().toString().trim());
                }
                break;
        }
    }

    class TimeCount extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getVerificationCodeTv.setEnabled(false);
            long time = millisUntilFinished % 1000 > 500 ? millisUntilFinished / 1000 + 1 : millisUntilFinished / 1000;
            getVerificationCodeTv.setText(String.valueOf(time + "秒"));
        }

        @Override
        public void onFinish() {
            getVerificationCodeTv.setText("重新获取");
            getVerificationCodeTv.setEnabled(true);
        }
    }

}
